#!/usr/bin/env python2.2

import getopt
import random
import sys
import warnings

import scrod
import cprdsp_fin

def build_cmdid(row, col, ch, win, event):
    return 0

def log_word(fp, w):
    fp.write(hex(w) + '\n')
        

def main():
    """
    send-ordered-packets.py [opts]

    Send words from 0 to some number and see what returns.
    Ideally the DSP_FIN will be hooked up to a SCROD on the inner transceiver
    so it is operating in loopback mode.

    Options:
    ---------------------------------------------------------
      -h              print this message
      -o PATH         write sent packet data to PATH
    """

    optl, args = getopt.getopt(sys.argv[1:], "ho:n:")
    
    optd = dict(optl)

    if '-h' in optd:
        sys.stderr.write(main.__doc__ + '\n')
        
        sys.stderr.flush()
        return -101

    if '-n' in optd:
        optd['-n'] = int(optd['-n'])
    else:
        optd['-n'] = 1000

    if '-o' not in optd:
        optd['-o'] = sys.stdout
    else:
        optd['-o'] = open(optd['-o'], 'w')

    if len(args) == 0:
        msg = "Please supply the number of words you wish to send."
        
    slots = 'a'
    warnings.warn("Currently hardcoded to talk to `a:0'")

    for i in xrange(optd['-n']):
        log_word(optd['-o'], i)
        cprdsp_fin.send_packets(slots, [i])    
                    
    if optd['-o'] != sys.stdout:
        optd['-o'].close()

    return 0


if __name__ == "__main__":
    sys.exit(main())
